package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleMapper roleMapper;
    @Override
    public PageResult<Role> getRoles(Role role) {
        PageHelper.startPage(role.getPage(), role.getPageSize());
        List<Role> result = roleMapper.getRoles(role);
        Page<Role> pageInfo = (Page<Role>) result;
        return PageResult.<Role>builder()
                .total(Long.valueOf(pageInfo.getTotal()).intValue())
                .rows(pageInfo.getResult())
                .build();
    }

    @Override
    public void deleteRole(Integer id) {roleMapper.deleteRole(id);}

    @Override
    public void addRole(Role role) {
        boolean hasNull = BeanUtil.hasNullField(role, "id", "name", "label", "updateTime", "createTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        role.setUpdateTime(LocalDateTime.now());
        role.setCreateTime(LocalDateTime.now());
        roleMapper.addRole(role);
    }

    @Override
    public Role getRoleById(Integer id) {return roleMapper.getRoleById(id);}

    @Override
    public void updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }

    @Override
    public List<Role> list() {
        return roleMapper.selectAll();
    }

}
